unit uLBMPTrackClient;

interface

uses
  SysUtils, uDZSysUtils, uDZPersistent,
  DLSocketComponent, DLSocketClient, DLTCPClient,
  DLSocketServer, DLTCPServer, DLSocketHandlerV2, DLTCPStream,
  uCxtConst, uGxtTypes, uGxtSvcIntf, uCLDWProtocol, uAppConfig, uAppLog;

type
  TDWTrackDB = class(TDzInterfacedObject, ILbmpTrackDatabase)
  private
    fPort: Word;
    fPassword: AnsiString;
    fServerIP: AnsiString;
    fUserName: AnsiString;
  public
    procedure Add(const Phone: AnsiString; LocType: TLbmpLocType; LocTime: TDateTime;
      CityID: Integer; const Longitude, Latitude: AnsiString; const Address: WideString;
      Param: Pointer; Callback: TSimpleCallback);

    procedure Get(const Phone: AnsiString; TrackList: TDzPersistentList;
      Param: Pointer; Callback: TSimpleCallback);
      
    property Host: AnsiString read fServerIP write fServerIP;
    property Port: Word read fPort write fPort;
    property UserName: AnsiString read fUserName write fUserName;
    property Password: AnsiString read fPassword write fPassword;
  end;

function SaveLBMPTrack(list: TCLDWItemList): Boolean;
function GetLBMPTrack(const Phone: AnsiString; Count: Integer): TCLDWItemList;

implementation

function SaveLBMPTrack(list: TCLDWItemList): Boolean;
var
  request: TCLDWRequest;
  response: TCLDWResponse;
  client: TDLTCPClient;
  stream: TDLTCPStream;
begin
  client := TDLTCPClient.Create(nil);
  with AppConfig do 
  try
    client.RemoteAddr := LBMPTrackServerAddr;
    client.RemotePort := LBMPTrackServerPort;
    client.Active := True;
    request := TCLDWRequest.Create;
    try
      request.RequestType := 0;
      request.Time := DateTimeToFileDate(now);
      request.User := DWTrackServerUserName;
      request.Pass := DWTrackServerPassword;
      request.RequestType:= 0;
      request.Request := list.Data;
      request.VerifyCode := request.CalVerifyCode;
      client.Send(request.Data);
      stream := TDLTCPStream.Create(client.Handler);
      response := TCLDWResponse.Create;
      try
        response.LoadFromStream(stream);
        Result := response.ResultCode = 0;
      finally
        stream.Free;
        response.Free;
      end;
    finally
      request.Free;
    end;
  finally
    client.Free;
  end;
end;

function GetLBMPTrack(const Phone: AnsiString;
  Count: Integer): TCLDWItemList;
var
  request: TCLDWRequest;
  response: TCLDWResponse;
  query: TCLDWQuery;
  client: TDLTCPClient;
  stream: TDLTCPStream;
begin
  client := TDLTCPClient.Create(nil);
  with AppConfig do
  try
    client.RemoteAddr := LBMPTrackServerAddr;
    client.RemotePort := LBMPTrackServerPort;
    client.Active := True;
    query := TCLDWQuery.Create;
    request := TCLDWRequest.Create;
    try
      query.Phone := Phone;
      query.Count := Count;
      request.Time := DateTimeToFileDate(now);
      request.User := DWTrackServerUserName;
      request.Pass := DWTrackServerPassword;
      request.RequestType:= 1;
      request.Request := query.Data;
      request.VerifyCode := request.CalVerifyCode;
      client.Send(request.Data);

      stream := TDLTCPStream.Create(client.Handler);
      response := TCLDWResponse.Create;
      try
        response.LoadFromStream(stream);
        if response.ResultCode = 0 then
        begin
          Result := TCLDWItemList.Create;
          try
            Result.Data := response.ResultContent;
          except
            Result.Free;
            Result := nil;
          end;
        end
        else Result := nil;
      finally
        stream.Free;
        response.Free;
      end;
    finally
      query.Free;
      request.Free;
    end;
  finally
    client.Free;
  end;
end;

{ TDWTrackDB }

procedure TDWTrackDB.Add(const Phone: AnsiString; LocType: TLbmpLocType;
  LocTime: TDateTime; CityID: Integer; const Longitude, Latitude: AnsiString;
  const Address: WideString; Param: Pointer; Callback: TSimpleCallback);
var
  list: TCLDWItemList;
  item: TCLDWItem;
  Result: TFuncCallResult;
begin
  list := TCLDWItemList.Create;
  try
    item := TCLDWItem(list.Add);
    item.Phone := Phone;
    item.LocType := Ord(LocType) + 1;
    item.AddTime := Now;
    item.CityCode := CityID;
    item.JingDu := StrToFloatDef(Longitude, 0);
    item.WeiDu := StrToFloatDef(Latitude, 0);
    item.WZMS := Address;
    try
      SaveLBMPTrack(list);
      Result.ErrCode := 0;
      Result.ErrMsg := '';
    except
      Result.ErrCode := CXT_E_DBENGINE;
      Result.ErrMsg := SDatabaseError;   
    end;
    Callback(Result, Param);
  finally
    list.Free;
  end;
end;

procedure TDWTrackDB.Get(const Phone: AnsiString; TrackList: TDzPersistentList;
  Param: Pointer; Callback: TSimpleCallback);
var
  ts: TCLDWItemList;
  ti: TCLDWItem;
  li: TLBMPTrackItem;
  i: Integer;
  Result: TFuncCallResult;
begin
  ts := uLBMPTrackClient.GetLBMPTrack(Phone, 100);
  if  Assigned(ts) then
  begin
    Result.ErrCode := CXT_E_DBENGINE;
    Result.ErrMsg := SDatabaseError;
    Callback(Result, Param);
    Exit;
  end;
  try
    for i := 0 to ts.Count - 1 do
    begin
      ti := TCLDWItem(ts.Items[i]);
      li := TLBMPTrackItem(TrackList.Add);
      li.LocType := TLbmpLocType(ti.LocType - 1);
      li.Position := ti.WZMS;
      li.Latitude := FloatToStr(ti.WeiDu);
      li.Longitude := FloatToStr(ti.JingDu);
      li.LocateTime := ti.AddTime;
      li.Address := ti.WZMS;
      li.Position := ti.WZMS;
    end;
    Result.ErrCode := 0;
    Result.ErrMsg := '';
    Callback(Result, Param);
  finally
    ts.Free;
  end;
end;

end.
